﻿#include "VAOneXMLAdaptorMaterialFluid.h"
#include "rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITKAcousticsPhysics/FITKAcousticsMaterialsFluid.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"


namespace IO
{
    bool VAOneXMLAdaptorMaterialFluid::adaptR()
    {
        // 返回操作是否成功
        return readMaterialFluid();
    }

    bool VAOneXMLAdaptorMaterialFluid::adaptW()
    {
        // 返回操作是否成功
        return writeMaterialFluid();
    }

    bool VAOneXMLAdaptorMaterialFluid::readMaterialFluid()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        auto nameNode = _node->first_node("Name")->first_node();

        if (nameNode == nullptr)
        {
            //材料流体数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "MaterialFluid name is null");
            return false;
        }

        Acoustics::FITKAcousticsMaterialsFluid* fluid = dynamic_cast<Acoustics::FITKAcousticsMaterialsFluid*>(_dataObj);

        if(!fluid)
        {
            return false;
        }

        //数据读取状态 默认为true
        bool b = true;
        // 名称字符串
        QString strName;
        strName = readStrNodeValue(nameNode, &b);

        if(b)
        {
            fluid->setDataObjectName(strName);
        }

        // 读取各项属性
        // 每个属性可能存在缺省配置
        double dValue = readDoubleAttribute(_node, "density", &b);// 读取密度

        if(b)
        {
            fluid->setDensity(dValue);
        }

        double kvValue = readDoubleAttribute(_node, "kinematic_viscosity", &b);// 读取运动粘度

        if(b)
        {
            fluid->setKinematicViscosity(kvValue);
        }


        double pnValue = readDoubleAttribute(_node, "prandtl_number", &b);// 读取普朗特数

        if(b)
        {
            fluid->setPrandtlNumber(pnValue);
        }

        double shrValue = readDoubleAttribute(_node, "specific_heats_ratio", &b);//读取比热比

        if(b)
        {
            fluid->setSpecificHeatsRatio(shrValue);
        }

        double sosValue = readDoubleAttribute(_node, "speed_of_sound", &b); //读取声速

        if(b)
        {
            fluid->setSpeedOfSound(sosValue);
        }

        double mValue = readDoubleAttribute(_node, "molecularMass", &b); //读取摩尔质量

        if(b)
        {
            fluid->setMolecularMass(mValue);
        }

        int typeValue = readIntAttribute(_node, "type", &b);//读取流体类型

        if(b)
        {
            // 设置流体类型
            Acoustics::FITKAcousticsMaterialsFluid::FluidType spectraType = static_cast<Acoustics::FITKAcousticsMaterialsFluid::FluidType>(typeValue);
            fluid->setType(spectraType);
        }

        // 读取备注
        auto note_node = _node->first_node("Note");
        if(note_node)
        {
            auto note = note_node->first_node();

            if (note != nullptr)
            {
                QString strNote = readStrNodeValue(note, &b);

                if(b)
                {
                    fluid->setMaterialDescribe(strNote);
                }
            }
        }
        //读取成功
        return true;
    }

    bool VAOneXMLAdaptorMaterialFluid::writeMaterialFluid()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        Acoustics::FITKAcousticsMaterialsFluid* fluid = dynamic_cast<Acoustics::FITKAcousticsMaterialsFluid*>(_dataObj);

        if (!fluid)
        {
            return false;
        }

        // 写入各项属性
        writeAttribute(_node, "density", fluid->getDensity());
        writeAttribute(_node, "speed_of_sound", fluid->getSpeedOfSound());
        writeAttribute(_node, "kinematic_viscosity", fluid->getKinematicViscosity());
        writeAttribute(_node, "prandtl_number", fluid->getPrandtlNumber());
        writeAttribute(_node, "specific_heats_ratio", fluid->getSpecificHeatsRatio());
        writeAttribute(_node, "type", fluid->getType());
        writeAttribute(_node, "molecularMass", fluid->getMolecularMass());
        // 创建名称节点
        createNodeElement(_node, "Name", fluid->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", fluid->getMaterialDescribe());
        //数据写入成功
        return true;
    }
}
